import Types from '../types'
import {apiUrl} from "../../../config";

/**
 * 获取注册验证码
 * @param
 * @returns
 */
export function fetchRegistAuthCode(params) {
  return dispatch => {
    dispatch({type: Types.AUTH_CODE_GET_READY});
    let url = `${apiUrl}/patriarchController/sendAuthCode`;
    fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type':'multipart/form-data',
      },
      body: params
    }).then(res => {
      let data = res._bodyText;
      data = JSON.parse(data)
      console.log(data)
      if(data.success){
        dispatch({
          type: Types.AUTH_CODE_GET_SUCCESS,
          authCodeTip: data.msg,
        })
      }
    }).catch(error => {
      console.log(error)
      dispatch({
        type: Types.AUTH_CODE_GET_FAIL,
        error
      })
    })
  }
}

/**
 * 用户注册
 *
 */
export function onRegist(params){
  return dispatch => {
    dispatch({type: Types.REGIST_READY});
    let url = `${apiUrl}/patriarchController/register`;
    fetch(url, {
      method: "POST",
      headers: {
        'Content-Type': 'multipart/form-data',
      },
      body: params
    }).then(res => {
      console.log(res)
      let data = res._bodyText;
      data = JSON.parse(data)
      console.log(data)
      if(data.success){
        dispatch({
          type: Types.REGIST_SUCCESS,
          registTip: data.msg,
          registStatus: data.success,
        })
      }
    }).catch(error => {
      console.log(error)
      dispatch({
        type: Types.REGIST_FAIL,
        error
      })
    })
  }
}
